package com.fmi.timedealer.reporting;

import java.util.List;

import com.fmi.timedealer.R;
import com.fmi.timedealer.reporting.model.CustomReportItem;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ReportsTableActivity extends Activity{

	private List<CustomReportItem> reportItems;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report_table);
		
		reportItems = CustomReportActivity.getReportItems();
		
		TextView lblInfo = (TextView) findViewById(R.id.lbl_Info);

		if (reportItems == null || reportItems.isEmpty()) {
			lblInfo.setText("No activities to display");
		}
		else {
			lblInfo.setText("Report");
			initTableWithActivities();
		}
	}
	
	/**
	 * Create and populate a table with all activities from DB
	 */
	private void initTableWithActivities() {
		
		TableLayout table = (TableLayout) findViewById(R.id.report_table);
		//table.removeAllViews();
        TableRow columnsHeadersRow = new TableRow(this);
    	columnsHeadersRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      	
    	TextView nameHeaderTextView = new TextView(this); 
    	nameHeaderTextView.setText("Name");
    	nameHeaderTextView.setBackgroundColor(Color.DKGRAY);
    	nameHeaderTextView.setTypeface(Typeface.DEFAULT_BOLD);
    	nameHeaderTextView.setPadding(5, 5, 5, 5);
    	
    	TextView categoryHeaderTextView = new TextView(this); 
    	categoryHeaderTextView.setText("Category");
    	categoryHeaderTextView.setBackgroundColor(Color.DKGRAY);
    	categoryHeaderTextView.setTypeface(Typeface.DEFAULT_BOLD); 
    	categoryHeaderTextView.setPadding(5, 5, 5, 5);
    	
    	TextView startHeaderTextView = new TextView(this); 
    	startHeaderTextView.setText("Time Spent");
    	startHeaderTextView.setBackgroundColor(Color.DKGRAY);
    	startHeaderTextView.setTypeface(Typeface.DEFAULT_BOLD); 
    	startHeaderTextView.setPadding(5, 5, 5, 5);
    	
    	TextView endHeaderTextView = new TextView(this); 
    	endHeaderTextView.setText("Date");
    	endHeaderTextView.setBackgroundColor(Color.DKGRAY);
    	endHeaderTextView.setTypeface(Typeface.DEFAULT_BOLD); 
    	endHeaderTextView.setPadding(5, 5, 5, 5);
    	
    	columnsHeadersRow.addView(nameHeaderTextView);
    	columnsHeadersRow.addView(categoryHeaderTextView);
    	columnsHeadersRow.addView(startHeaderTextView);
    	columnsHeadersRow.addView(endHeaderTextView);
    	
    	table.addView(columnsHeadersRow);
    	int isEven = 1;
        
        for(CustomReportItem item : reportItems) {
        	TableRow row = createActivityTableRow(item, isEven);
        	isEven++;       	
        	table.addView(row);
        }
	}
	
	private TableRow createActivityTableRow(CustomReportItem item, int isEven) {
		TableRow row = new TableRow(this);
    	if(isEven % 2 == 0) {
    		row.setBackgroundColor(Color.GRAY);
    	}
    	row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      	
    	TextView nameTextView = new TextView(this); 
    	String name = item.getActivityName();
    	nameTextView.setText(name);
    	nameTextView.setTypeface(Typeface.SANS_SERIF);
    	nameTextView.setTextSize(9);
    	nameTextView.setPadding(5, 5, 5, 5);
    	
    	TextView categoryTextView = new TextView(this); 
    	String category =item.getActivityCategory();
    	categoryTextView.setText(category);
    	categoryTextView.setTypeface(Typeface.SANS_SERIF);
    	categoryTextView.setTextSize(9);
    	categoryTextView.setPadding(5, 5, 5, 5);
    	
    	TextView startDateTextView = new TextView(this); 
    	startDateTextView.setText(item.getTimeSpentInHours());
    	startDateTextView.setTypeface(Typeface.SANS_SERIF);
    	startDateTextView.setTextSize(9);
    	startDateTextView.setPadding(5, 5, 5, 5);
    	
    	TextView endDateTextView = new TextView(this); 
    	endDateTextView.setText(item.getDate().toGMTString());
    	endDateTextView.setTypeface(Typeface.SANS_SERIF);
    	endDateTextView.setTextSize(9);
    	endDateTextView.setPadding(5, 5, 5, 5);
    	
    	row.addView(nameTextView);
    	row.addView(categoryTextView);
    	row.addView(startDateTextView);
    	row.addView(endDateTextView);
    	    	
    	return row;
	}
}
